/*
 *  Copyright (C) 2000 Russell King
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */
#ifdef CONFIG_RTOS_HAL_SELFDECOMPRESS_ACPI
#include "image.h"
#endif

#include <asm/memory.h>

#ifdef CONFIG_CPU_ENDIAN_BE8
#define ZIMAGE_MAGIC(x) ( (((x) >> 24) & 0x000000ff) | \
			  (((x) >>  8) & 0x0000ff00) | \
			  (((x) <<  8) & 0x00ff0000) | \
			  (((x) << 24) & 0xff000000) )
#else
#define ZIMAGE_MAGIC(x) (x)
#endif

OUTPUT_ARCH(aarch64)
ENTRY(_start)
#ifdef CONFIG_RTOS_HAL_SELFDECOMPRESS_ACPI
PECOFF_FILE_ALIGNMENT = 0x200;
#define PECOFF_EDATA_PADDING	\
    .pecoff_edata_padding : { BYTE(0); . = ALIGN(PECOFF_FILE_ALIGNMENT); }
#else
#define PECOFF_EDATA_PADDING
#endif

SECTIONS
{
  /DISCARD/ : {
    *(.ARM.exidx*)
    *(.ARM.extab*)
    /*
     * Discard any r/w data - this produces a link error if we have any,
     * which is required for PIC decompression.  Local data generates
     * GOTOFF relocations, which prevents it being relocated independently
     * of the text/got segments.
     */
    *(.data)
  }
  . = TEXT_START;
#ifdef CONFIG_RTOS_HAL_SELFDECOMPRESS_ACPI
  .head.text : {
    _text = .;
    *(.head.text)
  }
#else
  _text = .;
#endif
  .text : {
    _start = .;
    *(.start)
    *(.text)
    *(.text.*)
    *(.fixup)
    *(.gnu.warning)
    *(.glue_7t)
    *(.glue_7)
  }
  .rodata : {
    *(.rodata)
    *(.rodata.*)
  }
  .piggydata : {
    *(.piggydata)
  }

  . = ALIGN(4);
  _etext = .;

  .got.plt		: { *(.got.plt) }
  .got : {
    _got_start = .;
    *(.got)
    _got_end = .;
  }

  /* ensure the zImage file size is always a multiple of 64 bits */
  /* (without a dummy byte, ld just ignores the empty section) */
  .pad			: { BYTE(0); . = ALIGN(8); }

#ifdef CONFIG_RTOS_HAL_SELFDECOMPRESS_ACPI
  .data : ALIGN(4096) {
    __pecoff_data_start = .;
    /*
     * The EFI stub always executes from RAM, and runs strictly before the
     * decompressor, so we can make an exception for its r/w data, and keep it
     */
    *(.init.data .init.bss)
    *(.data.efistub .bss.efistub)
    __pecoff_data_end = .;

    /*
     * PE/COFF mandates a file size which is a multiple of 512 bytes if the
     * section size equals or exceeds 4 KB
     */
    . = ALIGN(512);
  }
  PECOFF_EDATA_PADDING
	__pecoff_data_rawsize = ABSOLUTE(. - __pecoff_data_start);
#endif
  _edata = .;

  _magic_sig = ZIMAGE_MAGIC(0x016f2818);
  _magic_start = 0x016f2818;
  _magic_end = ZIMAGE_MAGIC(_edata);

  . = BSS_START;
  __bss_start = .;
  .bss			: { *(.bss) }
  _end = .;

  . = ALIGN(8);		/* the stack must be 64-bit aligned */
  .stack		: { *(.stack) }

#ifdef CONFIG_RTOS_HAL_SELFDECOMPRESS_ACPI
  . = ALIGN(SEGMENT_ALIGN);
  __pecoff_data_size = ABSOLUTE(. - __pecoff_data_start);
  __pecoff_end = .;
#endif

  .stab 0		: { *(.stab) }
  .stabstr 0		: { *(.stabstr) }
  .stab.excl 0		: { *(.stab.excl) }
  .stab.exclstr 0	: { *(.stab.exclstr) }
  .stab.index 0		: { *(.stab.index) }
  .stab.indexstr 0	: { *(.stab.indexstr) }
  .comment 0		: { *(.comment) }
}
